package com.qyx.pmpucat.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;


@Configuration
public class MyBatisFlexConfiguration implements EnvironmentAware, MyBatisFlexCustomizer {
    private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql");

    private static Environment env;

    @Override
    public void customize(FlexGlobalConfig flexGlobalConfig) {
        Boolean logPrint = env.getProperty("base-variables.sql-log-print", Boolean.class, false);
        //开启审计功能
        AuditManager.setAuditEnable(logPrint);
        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage ->
                logger.info(" 执行时间:{}ms->\n{}", auditMessage.getElapsedTime(), auditMessage.getFullSql())
        );
    }

    @Override
    public void setEnvironment( Environment environment) {
        env = environment;
    }

}